package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.activity.eActivityRechargeType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.task.args.ManorRechargeArgs;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = GameProtocol.S_ASSISTANT_RECHARGE_NOTICE, desc = "小助直接花钱触发condition")
public class AssistantRechargeNoticeCmd extends AbstractCommand {

    private int activityId;
    private int conditionType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        AssistantProto.AssistantRechargeNoticeReqMsg reqMsg = AssistantProto.AssistantRechargeNoticeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo != null && activityInfo.getType() == eActivityType.RMBMallActivity.getValue()){
            ActivityModule activityModule = player.getModule(ActivityModule.class);
            BaseActivityData baseActivityData = activityModule.getBaseActivityData(activityId);
            if (null == baseActivityData) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
            List<UserActivityConditionData> cList = baseActivityData.getUserActivityConditionDataList();
            //如果还有没过期的 不给触发
            for(UserActivityConditionData conditionData : cList){
                if (StringUtils.isNullOrEmpty(conditionData.getParam())){
                    continue;
                }

                if (!conditionData.isGetReward() && Long.parseLong(conditionData.getParam()) > System.currentTimeMillis() / 1000){
                    ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                    return;
                }
            }
            boolean notice = false;

            for(UserActivityConditionData conditionData : cList){
                if(conditionData.getType() == conditionType && conditionData.getValue().equals(BigInteger.ZERO)){
                    notice = true;
                    break;
                }
            }
            if(notice){
                String cost = GameConfig.GAME_MONEY_INGOTS + "=" + GameConfig.ASSISTANT_LIBAO_SHOW_COST;
                CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
                if(!currencyModule.currencyIsEnough(PropertyHelper.parseStringToProperty(cost))){
                    ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                    return;
                }
                currencyModule.removeCurrency(PropertyHelper.parseStringToProperty(cost), eLogMoneyType.Activity,eLogMoneyType.ActivityRechargeNotice);
                if(conditionType == eGamePlayerEventType.RechargeManor.getValue()){
                    player.notifyListener(eGamePlayerEventType.RechargeManor.getValue(), new ManorRechargeArgs(1, 0));
                }else{
                    player.notifyListener(conditionType, -666);
                }
            }else{
                ret = GameErrorCode.E_ASSISTANT_RECHARGE_HAS_NOTICE;
                return;
            }
        }else{
            ret = GameErrorCode.E_ASSISTANT_RECHARGE_NO_SUPPORT;
            return;
        }
        /*if(eActivityRechargeType.has(conditionType)){

        }*/
    }

    @Override
    public void sendData(GamePlayer player) {
        AssistantProto.AssistantRechargeNoticeRespMsg.Builder respMsg = AssistantProto.AssistantRechargeNoticeRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_ASSISTANT_RECHARGE_NOTICE, respMsg);
    }
}
